package com.example.filecollectionsystemdemo1.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.filecollectionsystemdemo1.common.AuthAccess;
import com.example.filecollectionsystemdemo1.common.Result;
import com.example.filecollectionsystemdemo1.entity.Users;
import com.example.filecollectionsystemdemo1.exception.ServiceException;
import com.example.filecollectionsystemdemo1.service.UsersService;
import com.example.filecollectionsystemdemo1.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Users)表控制层
 *
 * @author makejava
 * @since 2024-10-08 17:03:31
 */
@CrossOrigin
@RestController
@RequestMapping("users")
public class UsersController {

    @Autowired
    UsersService usersService;


    /**
     * 查询全部
     */
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Users> usersList = usersService.list(new QueryWrapper<Users>().orderByDesc("UserID"));
        return Result.success(usersList);
    }

    /**
     * 根据ID查询用户信息
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Users users = usersService.getById(id);
        return Result.success(users);
    }

    @GetMapping("/currentUserInfo")
    public Result getCurrentUserInfo() {
        Users  currentUser = usersService.getCurrentUser();
        return Result.success(currentUser);
    }

    //根据id查询用户真实姓名
    @GetMapping("/selectNameById/{id}")
    public Result selectNameById(@PathVariable Integer id) {
        Users users = usersService.getById(id);
        return Result.success(users.getName());
    }


    /**
     * 多条件模糊查询用户信息
     * pageNum 当前的页码
     * pageSize 每页查询的个数
     */
    @AuthAccess
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String username
                             ) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>().orderByDesc("UserID");
        queryWrapper.like(StrUtil.isNotBlank(username), "Username", username);
        // select * from user where username like '%#{username}%' and name like '%#{name}%'
        Page<Users> page = usersService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Users users) {
        try {
            usersService.save(users);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result update(@RequestBody Users users) {
        usersService.updateById(users);
        return Result.success();
    }



    /**
     * 删除
     */

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        usersService.removeById(id);
        return Result.success();
    }
    /**
     * 批量删除用户信息
     */

    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {  //  [7, 8]
        Users currentUser = TokenUtils.getCurrentUser();
        if(currentUser != null && currentUser.getUserid() != null && ids.contains(currentUser.getUserid())){
            throw new ServiceException("不能删除当前用户信息");
        }
        usersService.removeBatchByIds(ids);
        return Result.success();
    }
}


